package com.it.product.comtroller;

import com.it.product.domain.ProductSpec;
import com.it.product.service.IProductSpecService;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * projectName: spzx-parent
 *
 * @author: 王宝贵
 * time: 2024/9/20 23:47 周五
 * description:
 */
@Tag(name = "商品规格")
@RequestMapping("/productSpec")
@RestController
public class ProductSpecController  extends BaseController {

    @Autowired
    IProductSpecService productSpecService;

    /**
     * 分页查询列表
     * @param productSpec 查询的条件
     * @return
     */
    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public TableDataInfo list( ProductSpec productSpec){
        //若伊的分页
        startPage();
        List<ProductSpec> productSpecList= productSpecService.findPage(productSpec);
        return getDataTable(productSpecList);
    }

    /**
     * 根据商品规格id查询商品规则
     * @param id
     * @return
     */
    @Operation(summary = "获取商品详细信息")
    @GetMapping("/{id}")
    public AjaxResult selectProductSpecById (@PathVariable Long id){
        ProductSpec productSpec = productSpecService.selectProductSpecById(id);
        return success(productSpec);
    }

    /**
     * 新增商品规格
     * @param productSpec
     * @return
     */
    @Operation(summary = "新增商品规格")
    @PostMapping
    public  AjaxResult save(@RequestBody ProductSpec productSpec){
        boolean save = productSpecService.save(productSpec);
        return toAjax(save);
    }

    /**
     * 修改商品规格
     * @param productSpec
     * @return
     */
    @Operation(summary = "修改商品规格")
    @PutMapping
    public  AjaxResult update(@RequestBody ProductSpec productSpec){
        return toAjax(productSpecService.updateById(productSpec));
    }

    /**
     * 删除商品规格
     * @param ids 要删除非商品规格的id
     * @return
     */
    @Operation(summary = "删除商品规格")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productSpecService.removeBatchByIds(Arrays.asList(ids)));
    }


}